﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections;

namespace SPWorks.Convert_Documents
{
    public partial class Convert_DocumentsUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SPWeb web = SPContext.Current.Web;
                ddlSourceLists.Items.Clear();
                ddlTargetLists.Items.Clear();
                foreach (SPList lib in web.Lists)
                {
                    if (lib.BaseTemplate == SPListTemplateType.DocumentLibrary || lib.BaseTemplate == SPListTemplateType.PictureLibrary)
                    {
                        string s = lib.Title + " : " + lib.BaseTemplate;
                        ListItem item = new ListItem(s, lib.Title);
                        ddlSourceLists.Items.Add(item);
                        ddlTargetLists.Items.Add(item);
                    }
                }
                ddlTypes.Items.Clear();
                ddlOverWrite.Items.Clear();

                ddlTypes.DataSource = Enum.GetValues(typeof(ConvertDocumentsClass.DocSaveFormat));
                ddlTypes.DataBind();

                ddlOverWrite.DataSource = Enum.GetValues(typeof(ConvertDocumentsClass.DocSaveBehaviour));
                ddlOverWrite.DataBind();
            }
        }

        protected void btnConvert_Click(object sender, EventArgs e)
        {
            ConvertDocumentsClass docs = new ConvertDocumentsClass();
            ArrayList filelist = new ArrayList();
            foreach (ListItem item in chkItems.Items)
            {
                if (item.Selected)
                    filelist.Add(item.Text);
            }
            docs.sourceLibrary = ddlSourceLists.SelectedValue;
            docs.destinationLibrary = ddlTargetLists.SelectedValue;

            JobId = docs.ConvertDocuments(SPContext.Current.Site, SPContext.Current.Web, filelist, (ConvertDocumentsClass.DocSaveFormat)Enum.Parse(typeof(ConvertDocumentsClass.DocSaveFormat), ddlTypes.SelectedValue), (ConvertDocumentsClass.DocSaveBehaviour)Enum.Parse(typeof(ConvertDocumentsClass.DocSaveBehaviour), ddlOverWrite.SelectedValue));
            showResult(JobId);
        }
        static Guid JobId;
        private void showResult(Guid jobId)
        {
            ConvertDocumentsClass docs = new ConvertDocumentsClass();
            lblResult.Text = DateTime.Now.ToString() + " - " + docs.getResult(jobId);
        }

        protected void btnCheckResult_Click(object sender, EventArgs e)
        {
            if (JobId != null)
            {
                showResult(JobId);
            }
        }

        protected void ddlSourceLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists.TryGetList(ddlSourceLists.SelectedValue);
            chkItems.Items.Clear();
            if (list != null)
            {
                foreach (SPListItem item in list.Items)
                {
                    chkItems.Items.Add(new ListItem(item.Name, item.ID.ToString()));
                }
            }
        }
    }
}
